'''
case Type :数据库系统
case Name ：主机创建表空间，备机不存在相应目录	备机自动生成相应目录，主备表空间数据一致
Create At: 2020-06-30
owner : @peilinqian
description:1、查看备机是否存在相应路径2、主机进行建表空间3、查看备机是否生成相应表空间及路径
history：
created： 2020/06/30  created by @peilinqian
'''

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case013.py start------------')
        self.rootNode = Node(node='PrimaryRoot')
        self.dbUserNode = Node(node='PrimaryDbUser')
        self.standbyrootNode = Node(node='Standby1Root')
        self.standbyrootNode2 = Node(node='Standby2Root')
        self.standbydbUserNode = Node(node='Standby1DbUser')
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.space_path = os.path.join(os.path.dirname(self.DB_INSTANCE_PATH), 'test_space')
        self.com = Common()
        self.Constant = Constant()

    def test_main(self):
        # 查看备机不存在相应表空间目录
        cmd = f'ls {self.space_path}'
        self.log.info(cmd)
        msg = self.standbyrootNode.sh(cmd).result()
        if self.Constant.NO_FILE_MSG not in msg:
            cmd = f'rm -rf {self.space_path}'
            self.log.info(cmd)
            msg = self.standbyrootNode.sh(cmd).result()
            self.assertEqual(msg, '')
        self.log.info('not exsit space_path')

        # 执行sql语句
        create_space_cmd = "create tablespace space_test owner {dbuser} location \'{path}\';".format(
            dbuser=self.dbUserNode.ssh_user, path=self.space_path)
        self.assertIn('CREATE TABLESPACE', self.primary_sh.executDbSql(create_space_cmd))

        # 执行查询备机存在相应表空间目录
        cmd = f'ls {self.space_path}'
        self.log.info(cmd)
        msg = self.standbyrootNode.sh(cmd).result()
        self.assertTrue(self.Constant.NO_FILE_MSG not in msg)

        # 执行查询备机存在相应表空间目录
        cat_space_cmd = r'\db ;'
        self.assertIn(self.space_path, self.primary_sh.executDbSql(cat_space_cmd))
        self.log.info('standbydb exist space')

    def tearDown(self):
        self.log.info("-----------------------------------this is tearDown----------------------------")
        # 删除表空间
        drop_space_cmd = 'DROP TABLESPACE space_test ;'
        self.primary_sh.executDbSql(drop_space_cmd)

        # 删除目录
        self.log.info("---------------------------删除备1表空间路径----------------------")
        cmd = f'rm -rf {self.space_path}'
        self.log.info(cmd)
        msg = self.standbyrootNode.sh(cmd).result()
        self.log.info(msg)
        self.log.info("---------------------------删除主表空间路径----------------------")
        msg = self.rootNode.sh(cmd).result()
        self.log.info(msg)
        self.log.info("---------------------------删除备2表空间路径----------------------")
        msg = self.standbyrootNode2.sh(cmd).result()
        self.log.info(msg)
